<template>
	<view style="width: 100%;height: 100vh;align-items: center;text-align: center;">正在进入质量系统</view>

	<!-- <web-view ref="web" :src="loadData.domainUrl" @message="handleMessage"></web-view> -->
</template>

<script>
	import CryptoJS from '@/uni_modules/crypto-js/crypto-js.js'
	import store from "@/store"

	import {
		decrypt,
		encrypt
	} from '@/common/js/des.js'

	export default {
		data() {
			return {
				loadData: {
					title: '',
					domainUrl: '',
					menuCode: '',
				},
				token: '',
				userInfo: {}
			}
		},
		methods: {
			handleMessage(msg) {
				console.log(msg);
			},
			decryptKunpeng(params, secretKey, ivBase64) {
				// 使用 SHA-256 将密钥转换为 32 字节（256 位）的密钥
				const keyBytes = CryptoJS.SHA256(secretKey);

				// Base64 解码 IV
				const iv = CryptoJS.enc.Base64.parse(decodeURIComponent(ivBase64));

				const result = Object.keys(params).reduce((pre, key) => {
					const value = decodeURIComponent(params[key])
					const parseValue = CryptoJS.AES.decrypt(value, keyBytes, {
						iv,
						mode: CryptoJS.mode.CBC,
						padding: CryptoJS.pad.Pkcs7,
					}).toString(CryptoJS.enc.Utf8);
					pre[key] = parseValue;
					return pre;
				}, {});
				return result;
			},
			openURL(href) {
				// #ifdef APP-PLUS
				plus.runtime.openURL(href) // plus.runtime.openWeb(href);
				// #endif

				// #ifdef H5
				window.open(href,'_self')
				// #endif

				// var u = navigator.userAgent;
				// var isAndroid = u.indexOf(“Android”) > -1 || u.indexOf(“Adr”) > -1; //android终端
				// var isiOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
				// if (isAndroid) { //android终端
				// 	let url = res.data.url;
				// 	window.open(url);
				// } else if (isiOS) { //ios终端
				// 	window.location.href = res.data.url;
				// }

			}

		},
		onLoad(data) {
			if (data && data.domainUrl && data.menuCode == 'web') {
				this.loadData = data

				this.token = decrypt(uni.getStorageSync("token"), 3)
				const base64Token = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(this.token));
				console.log('Encrypted:', base64Token);
				let huazhengData = uni.getStorageSync("huazhengData")
				this.loadData.domainUrl = this.loadData.domainUrl + "?token=" + encodeURIComponent(huazhengData.token) +
					"&iv=" + encodeURIComponent(huazhengData.iv) +
					"&account=" + encodeURIComponent(huazhengData.account) + "&tk=" + encodeURIComponent(base64Token)

				this.openURL(this.loadData.domainUrl);

				console.log("----------------------加密数据-------------------------------");
				console.log("huazhengData.iv：", huazhengData.iv);
				console.log("huazhengData.account：", huazhengData.account);
				console.log("huazhengData.token：", huazhengData.token);
				console.log("原始tk：", this.token);
				console.log("base64_tk：", base64Token);
				console.log("质量系统拼接地址：", this.loadData.domainUrl);
				console.log("----------------------解密 验证 开始-------------------------------");

				let option = {
					account: huazhengData.account
				}
				const result = this.decryptKunpeng(option, 'huanYuH51042', huazhengData.iv)
				console.log('解密结果：', result)
				let userCode = result.account.split('_')[0]
				console.log('解密用户编码：', userCode)
				const tokentk = CryptoJS.enc.Base64.parse(base64Token).toString(CryptoJS.enc.Utf8);
				console.log('解密tk：', tokentk)
				console.log("----------------------解密 结束-------------------------------");
			}


		}
	}
</script>

<style>

</style>